package com.boge.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.boge.common.utils.R;
import com.boge.modules.shop.entity.CatEntity;
import com.boge.modules.shop.entity.CattypeEntity;
import com.boge.modules.shop.entity.NewsEntity;
import com.boge.modules.shop.service.*;
import com.boge.modules.sys.controller.WebController;
import com.boge.modules.sys.entity.SysRoleEntity;
import com.boge.modules.sys.entity.SysUserEntity;
import com.boge.modules.sys.entity.SysUserRoleEntity;
import com.boge.modules.sys.service.SysRoleService;
import com.boge.modules.sys.service.SysUserRoleService;
import com.boge.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AppController {

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysUserRoleService userRoleService;


    @Autowired
    private CattypeService cattypeService;

    @Autowired
    private CatService catService;

    @Autowired
    private NewsService newsService;


    @RequestMapping("/auth/home/info")
    //@RequiresPermissions("house:tarea:list")
    public R list(@RequestParam Map<String, Object> params){
        // 查询 商品类别
        List<CattypeEntity> types = cattypeService.list();
        if(types != null){
            for (CattypeEntity type : types) {
                type.setImg(WebController.BASE_URL+type.getImg());
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("types",types);
        return R.ok().put("code",200).put("message","success")
                .put("data",map)
                ;
    }

    @RequestMapping("/auth/home/news")
    //@RequiresPermissions("house:tarea:list")
    public R news(@RequestParam Map<String, Object> params){
        List<NewsEntity> news = newsService.list(new LambdaQueryWrapper<NewsEntity>().eq(NewsEntity::getStatus,1));
        if(news != null){
            for (NewsEntity newsEntity : news) {
                String img = newsEntity.getImg();
                String video = newsEntity.getVideo();
                if(StringUtils.isNotBlank(img)){
                    newsEntity.setImg(WebController.BASE_URL+img);
                }
                if(StringUtils.isNotBlank(video)){
                    newsEntity.setVideo(WebController.BASE_URL+video);
                }
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("news",news);
        return R.ok().put("code",200).put("message","success")
                .put("data",map)
                ;
    }

    @RequestMapping("/auth/home/catByTypeId")
    //@RequiresPermissions("house:tarea:list")
    public R catByTypeId(@RequestParam Map<String, Object> params){
        String typeId = (String) params.get("typeId");
        List<CatEntity> cats = catService.list(new LambdaQueryWrapper<CatEntity>().eq(CatEntity::getTypeId, typeId));
        if(cats != null){
            for (CatEntity cat : cats) {
                cat.setImg(WebController.BASE_URL+cat.getImg());

            }
        }
        return R.ok().put("code",200).put("message","success")
                .put("cats",cats)
                ;
    }

    @RequestMapping("/auth/home/catsList")
    //@RequiresPermissions("house:tarea:list")
    public R catsList(@RequestParam Map<String, Object> params){
        List<CatEntity> list = catService.list();
        if(list != null){
            for (CatEntity catEntity : list) {
                catEntity.setImg(WebController.BASE_URL+catEntity.getImg());
            }
        }
        return R.ok().put("code",200).put("message","success")
                .put("cats",list)
                ;
    }


    @PostMapping("/auth/registerApp")
    @ResponseBody
    public R registerApp(@RequestBody SysUserEntity user){
        user.setStatus(1);
        //user.setPassword("123456");
        if(StringUtils.isBlank(user.getNickname())){
            user.setNickname(user.getUsername());
        }

        // 绑定角色
        userService.saveUser(user);
        QueryWrapper<SysRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("role_name","用户");
        List<SysRoleEntity> list = roleService.list(wrapper);
        if(list != null && list.size() > 0){
            SysRoleEntity sysRoleEntity = list.get(0);
            SysUserRoleEntity entity = new SysUserRoleEntity();
            entity.setUserId(user.getUserId());
            entity.setRoleId(sysRoleEntity.getRoleId());
            userRoleService.save(entity);
        }
        return R.ok();
    }


}
